package com.example.lots_of_demo.security;

import com.example.lots_of_demo.model.UserEnum;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author : LZJ
 * @description : code for LZJ
 * @date : 2021/2/11
 */
public class SecurityUser implements UserDetails, Serializable {

    /**
     *
     */
    private Long userId;
    private String username;
    private String password;
    private Set<String> permissions;
    private String enable;

    public SecurityUser(Long userId, String username, String password, String enable, Set<String> permissions) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.enable = enable;
        this.permissions = permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> permissioons = new HashSet<>();
        this.permissions.forEach((p) -> {
            permissioons.add(new SimpleGrantedAuthority(p));
        });
        return permissioons;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true ;
    }

    @Override
    public boolean isEnabled() {
        return UserEnum.UserEnable.IS_ENABLE.getCode().equals(enable);
    }
}
